﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Mgrs;
using System;
using System.Collections;
using Unity.VisualScripting;
using UnityEngine;

public class PlayerDash : PlayerBaseState {
	Timer cd;
	Timer _timer;
	Timer _makeShadowGap = new();
	public PlayerDash(PlayerController controller, PlayerStateEnum stateName, State<PlayerStateEnum> superFSM, StateFactory<PlayerStateEnum> factory) : base(controller, stateName, superFSM, factory) {

		AddConditionToSelf(() => _controller.SlideInput);
		_timer = new Timer();
		cd = new();
	}
	bool InCd;
	public override void Enter() {
		InCd = cd.Elapsed < _controller.ConfigData.DashCd;
		if (InCd) {
			_controller.StateData.IsDashing = false;
			_controller.StateData.IsSpecial = false;
			return;
		}
		//Debug.Log("Enter");
		AudioManager.Play(_controller.Effect, AudioStr.PlayerDashAudio);
		Time.timeScale = _controller.ConfigData.DashTimeScale;
		var multi = 1 / _controller.ConfigData.DashTimeScale;
		_controller.StateData.CanChangeDir = false;
		var dir = _controller.StateData.FaceDir;
		//Debug.Log($"{dir}");
		_controller.Velocity = new UnityEngine.Vector2(multi * _controller.ConfigData.DashSpeed * dir, 0);
		_controller.StateData.IsDashing = true;
		_timer.Reset();
		//Debug.Log("Dash动画");
		_controller.PlayAnim(PlayerStateEnum.Dash);
		_controller.SetGravity(0);
	}


	public override void Update() {
		base.Update();
		//后者防止突然撞飞
		if (InCd) {

			return;
		}
		if (_timer.Elapsed > _controller.ConfigData.DashTime || Mathf.Abs(_controller.Velocity.x) < 3) {
			_controller.StateData.IsDashing = false;
			_controller.StateData.IsSpecial = false;
			_controller.Velocity = Vector2.zero;
		}
		_controller.MakeShadowSyn(_makeShadowGap, _controller.ConfigData.DashShadowGap);

	}

	public override void Exit() {
		if (InCd) return;
		_controller.StateData.CanChangeDir = true;
		Time.timeScale = 1;
		_controller.ResetGravity();
		cd.Reset();
		//Debug.Log("Exit");

	}
}